﻿using System;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;

namespace V5_WinLibs.Core {
    /// <summary>  
    /// 图片下载  
    /// </summary>  
    public class ImageDownHelper {
        public ImageDownHelper() { }

        #region 私有方法
        /// <summary>  
        /// 获取图片标志  
        /// </summary>  
        private static string[] GetImgTag(string htmlStr) {
            Regex regObj = new Regex("<img.+?>", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            string[] strAry = new string[regObj.Matches(htmlStr).Count];
            int i = 0;
            foreach (Match matchItem in regObj.Matches(htmlStr)) {
                strAry[i] = GetImgUrl(matchItem.Value);
                i++;
            }
            return strAry;
        }

        /// <summary>  
        /// 获取图片URL地址  
        /// </summary>  
        private static string GetImgUrl(string imgTagStr) {
            string str = "";
            Regex regObj = new Regex("http://.+.(?:jpg|gif|bmp|png)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            foreach (Match matchItem in regObj.Matches(imgTagStr)) {
                str = matchItem.Value;
            }
            return str;
        }
        #endregion

        /// <summary>  
        /// 下载图片到本地  
        /// </summary>  
        /// <param name="strHTML">HTML</param>  
        /// <param name="path">路径</param>  
        /// <param name="nowyymm">年月</param>  
        /// <param name="nowdd">日</param>  
        public static string SaveUrlPics(string strHTML, string path) {
            string nowym = DateTime.Now.ToString("yyyy-MM");  //当前年月  
            string nowdd = DateTime.Now.ToString("dd");       //当天号数  
            path = path + nowym + "/" + nowdd;
            if (!Directory.Exists(path)) Directory.CreateDirectory(path);

            string[] imgurlAry = GetImgTag(strHTML);
            try {
                for (int i = 0; i < imgurlAry.Length; i++) {
                    string preStr = System.DateTime.Now.ToString() + "_";
                    preStr = preStr.Replace("-", "");
                    preStr = preStr.Replace(":", "");
                    preStr = preStr.Replace(" ", "");
                    WebClient wc = new WebClient();
                    wc.DownloadFile(imgurlAry[i], path + "/" + preStr + imgurlAry[i].Substring(imgurlAry[i].LastIndexOf("/") + 1));
                }
            }
            catch (Exception ex) {
                return ex.Message;
            }
            return strHTML;
        }

        /// <summary>
        /// 下载图片到本地
        /// </summary>
        /// <param name="imgUrl">图片远程路径</param>
        /// <param name="path">本地图片主目录</param>
        /// <returns></returns>
        public static string DownUrlPics(string imgUrl, string path)
        {
            string nowym = DateTime.Now.ToString("yyyy-MM");  //当前年月  
            string nowdd = DateTime.Now.ToString("dd");       //当天号数  
            path = path + nowym + "/" + nowdd;
            if (!Directory.Exists(path)) Directory.CreateDirectory(path);

            string preStr = System.DateTime.Now.ToString() + "_";
            preStr = preStr.Replace("-", "");
            preStr = preStr.Replace(":", "");
            preStr = preStr.Replace(" ", "");
            string newFileName = "/" + preStr + imgUrl.Substring(imgUrl.LastIndexOf("/") + 1);

            try
            {
                WebClient wc = new WebClient();
                wc.DownloadFile(imgUrl, path + newFileName);
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
            return newFileName;
        }
    }
}